/*
* Copyright (c) 2007, Nubo Technologies
* 
* All rights reserved.
* 
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
*    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*    * Neither the name of the Nubo Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.nubotech.gwt.oss.client.s3;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.Response;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;
import com.nubotech.gwt.oss.client.Bucket;
import com.nubotech.gwt.oss.client.BucketResult;
import com.nubotech.gwt.oss.client.BucketResultCallback;
import com.nubotech.gwt.oss.client.OnlineStorageServiceError;
import com.nubotech.gwt.oss.client.util.DateUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author jonnakkerud
 */
public class BucketHandler extends AwsS3CallbackHandler {
    
    private BucketResultCallback cb;    
    protected List bucketHolderResultList = new ArrayList();
    
    public BucketHandler(BucketResultCallback cb) {
        this.cb = cb;
    }
    
    public void onResponseReceived(Request request, Response response) {        
        BucketResult bucketResult;
        if (hasError(response)) {
            bucketResult = new BucketResult(bucketHolderResultList, createError(response));
        }
        else {
            Document doc = getXml(response);
        
            if (doc != null) {
                createBuckets(doc);
            }
            
            bucketResult = new BucketResult(bucketHolderResultList, null);
        }
        cb.onBucketResult(bucketResult);
        
    }
    
    public void createBuckets(Document doc) {
        Element bucketsElement = doc.getDocumentElement();
        // Must do this if you ever use a raw node list that you expect to be
        // all elements.
        XMLParser.removeWhitespace(bucketsElement);
        
        NodeList list = bucketsElement.getElementsByTagName("Bucket");
        for (int i = 0; i < list.getLength(); i++) {
            Node n = list.item(i);
            bucketHolderResultList.add(createBucket(n));
        }
    }
    
    private Bucket createBucket(Node bucketNode) {
        Bucket b = new Bucket();
        String name = getElementTextValue((Element) bucketNode, "Name");
        b.setName(name);
        String dateStr = getElementTextValue((Element) bucketNode, "CreationDate");
        Date creationDate = DateUtils.parseISODate(dateStr);
        b.setCreationDate(creationDate);
        return b;
    }

    public void onError(Request request, Throwable ex) {
        cb.onBucketResult(new BucketResult(bucketHolderResultList, new OnlineStorageServiceError(ex)));        
    }
 
}
